package com.luoqiz.es.transport.service;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

@Service
public class EsService {

	@Autowired
	private TransportClient transportClient;

	public void getEventList() {

		SearchRequestBuilder setSearchType = transportClient.prepareSearch("job").setTypes("type1")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);

		setSearchType.setQuery(QueryBuilders.multiMatchQuery("abc", "title").minimumShouldMatch("80%"));

		System.out.println("開始查詢獲取");
		// 设置分页
		SearchResponse searchResponse = setSearchType.setSize(10).get();
		SearchHits hits = searchResponse.getHits();
		SearchHit[] hits2 = hits.getHits();
		System.out.println(JSON.toJSONString(hits2));
	}

}
